/**
 * Chrowety
 * utils.time.js
 */

//optimized

utils = window.utils || {};

utils.time = {};

/**
 * Renderiza os timestamps
 * @param context {DOMElement} Contexto da pesquisa
 */
utils.time.search = function( root )
{
	var 
	elems = ( root && root.querySelectorAll ? root : document ).querySelectorAll('*[data-datetime]'),
	i = elems.length;
	
	while( i-- ) 
	{
		elems[i].innerText = utils.time.tweet( elems[i].getData('datetime') );
	}
};


utils.time.randomNumber = function()
{
	//return Math.ceil(Math.random() * (Math.random() * 9999999999));
	var n = Math.random() * 9999999999;
	return n === (n >>= 0) ? n : n + 1; // fast ceil, no negative allowed
};

/**
 * Seconds
 * @param seconds {Int}
 * @return milliseconds {Int}
 */
utils.time.s = function(seconds)
{
	//return Math.floor(seconds * 1e3);
	return (seconds *= 1e3) < 0 ? seconds === (seconds >>= 0) ? seconds : seconds - 1 : seconds;
};

/**
 * Minutes
 * @param minutes {Int}
 * @return milliseconds {Int}
 */
utils.time.m = function(minutes)
{
	// return Math.floor(minutes * 6e4);
	return (minutes *= 6e4) < 0 ? minutes === (minutes >>= 0) ? minutes : minutes - 1 : minutes;
};

/**
 * Retorna a data
 * @param datetime {String} Sun May 16 22:53:15 +0000 2010
 * @return data {String} dd/mm/yyyy
 */
utils.time.date = function(datetime)
{
	var a = new Date(datetime);
	return a.getDate() + '/' + (a.getMonth() + 1) + '/' + a.getFullYear();
};

/**
 * Retorna a hora
 * @param datetime {String} Sun May 16 22:53:15 +0000 2010
 * @return time {String} hh:mm:ss
 */
utils.time.hour = function(datetime)
{
	var a = new Date(datetime);
	return a.getHours() + ':' + a.getMinutes() + ':' + a.getSeconds();
};


/**
 * Retorna hora por unix time
 * @param unixTime {Int} 1279499838
 * @return time {String} hh:mm:ss
 */
utils.time.hourByUnixtime = function(unixTime)
{
	return this.hour(unixTime * 1000);
};

/**
 * Retorna a hora atual
 * @return time {String} hh:mm:ss
 */
utils.time.now = function( how )
{
	if (typeof how !== 'string') 
		how = 'hh:mm:ss';
	
	var 
	a = new Date(),
	h = a.getHours(),
	m = a.getMinutes(),
	s = a.getSeconds();
	
	return how
	.replace('hh', (h < 10 ? '0' + h : h) )
	.replace('mm', (m < 10 ? '0' + m : m) )
	.replace('ss', (s < 10 ? '0' + s : s) );
};

/**
 * Tweet Timestamp
 * @param datetime {String} Sun May 16 22:53:15 +0000 2010
 * @return strTime {String} Just now, Less than a minute ago, 1 minute ago e etc...
 */
utils.time.tweet = function(datetime)
{
	var T = (new Date().getTime() - Date.parse(new Date(datetime))) / 1000;
	var aux;
	if (T < 15) 
	{
		return i18n.get('just_now');
	}
	if (T < 60) 
	{
		return i18n.get('less_than_a_minute_ago');
	}
	if (T < 3660) // 60 * 60 
	{
		return 1 < (aux = T / 60 >> 0) ? i18n.get('minutes_ago', [aux]) : i18n.get('a_minute_ago');
	}
	if (T < 86400) //60 * 60 * 24 
	{
		return 1 < (aux = T / 3660 >> 0) ? i18n.get('hours_ago', [aux]) : i18n.get('a_hour_ago');
	}
	if (T < 2592000) // 60 * 60 * 24 * 30 
	{
		return 1 < (aux = T / 86400 >> 0) ? i18n.get('days_ago', [aux]) : i18n.get('a_day_ago');
	}
	if (T < 31104000) //60 * 60 * 24 * 30 * 12 
	{
		return 1 < (aux = T / 2592000 >> 0) ? i18n.get('months_ago', [aux]) : i18n.get('a_month_ago');
	}
	return i18n.get('years_ago');
	
};
